package com.renjibo.teawithmilkgoods.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.renjibo.teawithmilkgoods.Model.GoodsBean;
import com.renjibo.teawithmilkgoods.R;
import com.renjibo.teawithmilkgoods.ReFrecheRecycle;

import java.util.List;

/**
 * 任继波
 * Created by Administrator on 2017/2/15.
 */

public class HomeRecycleAdapter extends RecyclerView.Adapter<HomeRecycleAdapter.HomeViewHolder> {
    private Context context;
    private  List<GoodsBean.DataBean.CategoriesBean> categories;
    private ReFrecheRecycle reFrecheRecycle;
    private int theLastItem=0;
    private boolean isOneLogin=true;

    public HomeRecycleAdapter(GoodsBean goodsBean, Context context){
        this.categories = goodsBean.getData().getCategories();
        this.context=context;
    }
    public  void setReFreach(ReFrecheRecycle reFreach){
        this.reFrecheRecycle=reFreach;
    }
    @Override
    public HomeRecycleAdapter.HomeViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        final View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.home_recycle_item,parent,false);
        final HomeRecycleAdapter.HomeViewHolder viewHolder=new HomeRecycleAdapter.HomeViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final HomeViewHolder holder, final int position) {
//        if(position==0){
//            //holder.home_recycle_relativeLayout.setFocusable(true);
//            //holder.home_recycle_relativeLayout.setFocusableInTouchMode(false);
//
//            holder.home_recycle_relativeLayout.setBackgroundColor(Color.WHITE);
//        }
        String name = categories.get(position).getName();
        if(name.length()<4){
            holder.home_recycle_textView.setText(name+"    ");
        }else{
            holder.home_recycle_textView.setText(name);
        }
        holder.home_recycle_relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context,"点击了"+categories.get(position).getName(),Toast.LENGTH_SHORT).show();
                reFrecheRecycle.reFreach(position,v);

            }
        });
        if (position == 0) {
            if(isOneLogin){
                isOneLogin = false;
                holder.home_recycle_relativeLayout.performClick();
                holder.home_recycle_relativeLayout.setSelected(true);
            }
        } else {
            holder.home_recycle_relativeLayout.setSelected(false);
        }

    }



    @Override
    public int getItemCount() {
        return categories.size();
    }

    class HomeViewHolder extends RecyclerView.ViewHolder{
        TextView home_recycle_textView;
        RelativeLayout home_recycle_relativeLayout;
        public HomeViewHolder(View itemView) {
            super(itemView);
            home_recycle_textView= (TextView) itemView.findViewById(R.id.home_recycle_textView);
            home_recycle_relativeLayout= (RelativeLayout) itemView.findViewById(R.id.home_recycle_relativeLayout);
        }
    }
}
